#include "simport.h"

#include <QElapsedTimer>
#include "public/publicfunction.h"
#include <QThread>
#include <QElapsedTimer>

SimPort::SimPort()
{
    //init only required for windows, no-op on *nix
    //net::init();
}


SimPort::~SimPort()
{
    net::uninit();
}


/**
 * @brief SimPort::open
 * @param type
 * @param args
 * @return true-成功, false-失败
 */
bool SimPort::open(QString type, QStringList args)
{
    qDebug() << "SimPort::Open "  << type;

    if(type == "" || args.length() != 2)
        return false;
    return reOpen();
}

/**
 * @brief SimPort::reOpen
 * @return true-成功, false-失败
 */
bool SimPort::reOpen()
{
    return 1;
}

int SimPort::read(unsigned char *buf, int len, int ms)
{
    qDebug() << "SimPort::read";

    QElapsedTimer timer;
    timer.start();
    int t=0;
    QByteArray byteArray;
    while(timer.hasExpired(ms)){
        t++;
        byteArray.append(t);
        QThread::msleep(100);
    }
    for(int i=0; i<byteArray.size(); i++) {
        buf[i] = byteArray[i];
    }
    return byteArray.length();
}

int SimPort::write(unsigned char *buf, int len, int ms)
{
    qDebug() << "SimPort::write";
#if 0
    qDebug()<< "write: " << hexToString((char *)buf, len);
#endif
    return 0;
}


bool SimPort::close()
{
    return true;
}

TPortType SimPort::getPortType()
{
    return PORT_SIM;
}
